const gameDataConfig = require('../configs/gameDataConfig');
const CONST = require('../configs/const');

module.exports = class {
    constructor() {
        //总子弹加成
        this.m_bulletHurt = 1;
        //
        this.m_score = 0;
        //
        this.init();
        //
        this.gameState = CONST.GAME_STATE.FREE;
        //
        this.RankListDataArray = [];
    }

    init() {
        this.userLayer = cc.find('Canvas/layerGame/gameLayerUser');
        //
        this.infoNode = cc.find('info', this.userLayer);
        this.infoNode.active = false;
        this.lbScore = cc.find('info/lbScore', this.userLayer);
        //
        this.btnMenus = cc.find('layeroutMenu', this.userLayer);
        this.btnMenus.active = false;
        //
        this.ndLoading = cc.find('lbLoading', this.userLayer);
        this.ndLoading.active = true;
        //
        this.overLayer = cc.find('layerOver', this.userLayer);
        this.overLayer.actvie = false;
        this.rankCommit = this.overLayer.getChildByName('rankCommit');
        //暂停
        this.pauseLayer = cc.find('layerPause', this.userLayer);
        this.pauseLayer.active = false;
        //设置
        this.setLayer = cc.find('layerSet', this.userLayer);
        this.setLayer.active = false;
        //帮助
        this.helpLayer = cc.find('layerHelp', this.userLayer);
        this.helpLayer.active = false;
        //排行榜
        this.RankLayer = cc.find('layerRank', this.userLayer);
        this.RankLayer.active = false;
        this.RankListNode = cc.find('rankScrollView/view/content', this.RankLayer);
        //
        cc.game.on(cc.game.EVENT_SHOW, () => {
        });

        cc.game.on(cc.game.EVENT_HIDE, () => {
            if (this.gameState == CONST.GAME_STATE.GAME) {
                this.onPause();
            }
        });
    }

    setBulletHurt(hurt) {
        this.m_bulletHurt = hurt;
    }

    getBulletHurt() {
        const curHurt = ICPC.bulletManager.getBulletLevelInfo().hurt;
        //
        return this.m_bulletHurt * curHurt;
    }

    addScore(score, show = true) {
        this.m_score += score;
        show && this.updateScore();
    }

    updateScore() {
        //
        this.lbScore.getComponent(cc.Label).string = this.m_score;
        //是否升级炮台
        ICPC.bulletManager.onScoreChange(this.m_score);
    }

    //一轮敌人清除
    onEnemySuccess() {
        //怪下一化
        ICPC.enemyManager.enemyLevelUp();
        //出怪吧
        ICPC.enemyManager.startEnemy();
    }

    onLoadAllEnd(data) {
        this.btnMenus.active = true;
        this.ndLoading.active = false;
        //
        ICPC.soundManager.playerFreeBGM();
        //先获取一次
        ICPC.httpManager.getRankList(this.OnHttpRankList.bind(this));
    }

    startGame() {
        cc.director.getPhysicsManager().enabled = true;
        //
        this.btnMenus.active = false;
        this.infoNode.active = true;
        ICPC.bulletManager.startFire();
        ICPC.enemyManager.startEnemy();
        ICPC.flyManager.setFly();
        //
        this.gameState = CONST.GAME_STATE.GAME;
        ICPC.soundManager.playerGameBGM();
    }

    onPause() {
        this.pauseLayer.active = true;
        cc.director.getPhysicsManager().enabled = false;
        ICPC.bulletManager.stopFire();
    }

    onContinue() {
        this.pauseLayer.active = false;
        cc.director.getPhysicsManager().enabled = true;
        ICPC.bulletManager.startFire();
    }

    onGameOver() {
        cc.director.getPhysicsManager().enabled = false;
        //
        ICPC.bulletManager.stopFire();
        //
        this.overLayer.active = true;
        cc.find('layoutInfo/lbScore', this.overLayer).getComponent(cc.Label).string = `分数:${this.m_score}`;
        //
        this.gameState = CONST.GAME_STATE.OVER;
        //
        this.isUpRank();
    }

    continueGame() {
        //先提交分数
        this.isUpRank(true);
        //
        this.m_score = 0;
        this.updateScore();
        this.overLayer.active = false;
        this.infoNode.active = true;
        this.btnMenus.active = true;
        ICPC.bulletManager.clearAll();
        ICPC.enemyManager.clearAll();
        ICPC.flyManager.clearFly();
        //
        this.gameState = CONST.GAME_STATE.OVER;
        ICPC.soundManager.playerFreeBGM();
    }

    isUpRank(isCommit = false) {
        this.rankCommit.active = false;
        let isUp = false;
        for (let i = 0; i < CONST.RANK_LENGTH; i++) {
            if (this.RankListDataArray[i] == null) {
                isUp = true;
                break;
            }
            if (this.RankListDataArray[i]['score'] < this.m_score) {
                isUp = true;
                break;
            }
        }
        //上分
        if (isUp) {
            this.rankCommit.active = true;
            //
            if (isCommit) {
                //
                let name = this.rankCommit.getChildByName('editName').getComponent(cc.EditBox).string;
                name = name || '无名英雄';
                ICPC.httpManager.commitRank(name, this.m_score, () => {
                    ICPC.effectManager.showMessage('分数已同步到排行榜');
                });
            }
        }
    }

    onSetClick() {
        this.setLayer.active = !this.setLayer.active;
    }

    onHelpClick() {
        this.helpLayer.active = !this.helpLayer.active;
    }

    onRankClick() {
        this.RankLayer.active = !this.RankLayer.active;
        //
        if (this.RankLayer.active) {
            //刷新排行
            ICPC.httpManager.getRankList(this.OnHttpRankList.bind(this));
        }
    }

    //排行到
    OnHttpRankList(data) {
        this.RankListDataArray = data;
        // //先清空
        // for (let i = 1; i < this.RankListNode.childrenCount; i++) {
        //     this.RankListNode.children[i].destroy();
        // }
        //再加入
        for (let i = 0; i < data.length; i++) {
            let newNode = this.RankListNode.children[i + 1];
            if (newNode == null) {
                newNode = cc.instantiate(this.RankListNode.children[0]);
                this.RankListNode.addChild(newNode);
            }
            //
            newNode.getChildByName('lbName').getComponent(cc.Label).string = data[i]['name'];
            newNode.getChildByName('lbScore').getComponent(cc.Label).string = data[i]['score'];
            newNode.getChildByName('lbTime').getComponent(cc.Label).string = data[i]['time'];
        }
    }

    OnCheatPlus() {
        if (this.egg == null) {
            this.egg = 0;
        }
        this.egg++;
    }
}